package tt.dz.service.imp.base;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import tt.dz.dao.ext.base.OmsStoreFileDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsStoreFile;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.base.FileUploadService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
/**
 * <p>本地上传Service </p>
 * @author Administrator
 *
 */
@Service(value="localFileService") @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class LocalFileServiceImpl extends FileBaseService implements FileUploadService
{
	@Autowired
	public LocalFileServiceImpl(	OmsStoreFileDao<OmsStoreFile> storeFileDaoImpl)
	{
		super();
		super.setStoreFileDaoImpl(storeFileDaoImpl);
	}

	@Override
	public JsonCommon<?> addFileUpload(MultipartFile file,String fileFuns,String entity,String prefixPath,
			String targerDir,String fileRequstPrefixPath,String createId,String fileId,String entityId) 
	{
		StringBuilder filePath = new StringBuilder();
		filePath.append(prefixPath).append("/").append(targerDir);
		File filesDir = new File(filePath.toString());
		if (!filesDir.exists())
		{
			boolean mkflag = filesDir.mkdirs();
			if (!mkflag)
			{
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:do not create folder");
			}
		}
		String folder = filePath.append("/").toString();
		try 
		{
			long current = System.currentTimeMillis();
			StringBuilder remoteName = new StringBuilder();
			remoteName.setLength(0);
			remoteName.append(current).append(".").append(getExtensionName(file.getOriginalFilename()));
			filePath.setLength(0);
			filePath.append(folder).append(remoteName);
			file.transferTo(new File(filePath.toString()));
			filePath.setLength(0);
			filePath.append(fileRequstPrefixPath).append(targerDir).append("/").append(current).append(".").append(getExtensionName(file.getOriginalFilename()));
			OmsStoreFile obj = this.genarateObj(fileId,entity, fileFuns, createId, file.getOriginalFilename(), filePath.toString(), remoteName.toString(),entityId);
			if (StringUtils.isEmpty(fileId))
			{
				this.saveFile(obj);
			}
			else
			{
				this.updateFile(obj);
			}
			return this.buildSuccessJsonCommon(obj);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		} 
	}

	@Override
	public JsonCommon<?> addFileoUpload(MultipartFile file,String fileFuns,String prefixPath,String targerDir,String fileRequstPrefixPath) {
		StringBuilder filePath = new StringBuilder();
		filePath.append(prefixPath).append("/").append(targerDir);
		File filesDir = new File(filePath.toString());
		if (!filesDir.exists())
		{
			boolean mkflag = filesDir.mkdirs();
			if (!mkflag)
			{
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:do not create folder");
			}
		}
		String folder = filePath.append("/").toString();
		try 
		{
			long current = System.currentTimeMillis();
			StringBuilder remoteName = new StringBuilder();
			remoteName.setLength(0);
			remoteName.append(current).append(".").append(getExtensionName(file.getOriginalFilename()));
			filePath.setLength(0);
			filePath.append(folder).append(remoteName);
			file.transferTo(new File(filePath.toString()));
			filePath.setLength(0);
			filePath.append(fileRequstPrefixPath).append(targerDir).append("/").append(current).append(".").append(getExtensionName(file.getOriginalFilename()));
			OmsStoreFile obj = this.genarateObj(null,null, fileFuns, null, file.getOriginalFilename(), filePath.toString(), remoteName.toString(),null);
			return this.buildSuccessJsonCommon(obj);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		} 
	}

	@Override
	public JsonCommon<?> addFile(String fileName, String remoteFileName, String downloadPath, String entityTbl,
			String entityId, String fileFunction,String createId) 
	{
		OmsStoreFile storeFile = new OmsStoreFile(); 
//		storeFile.setFile_id(fileId);
		storeFile.setDownload_path(downloadPath);
		storeFile.setRemote_file_name(remoteFileName);
		storeFile.setCreate_oper_id(createId);
		storeFile.setCreate_time(new Date());
		storeFile.setFilename_extension(getExtensionName(fileName));
		storeFile.setFile_name(fileName);
		storeFile.setEntity_tbl(entityTbl);
		storeFile.setValid_flag("1");
		storeFile.setFile_function(fileFunction);
		storeFile.setEntity_id(entityId);
		try 
		{
			this.saveFile(storeFile);
		} 
		catch (Exception e) 
		{
//			e.printStackTrace();
			this.logError("add file errror", e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon(storeFile) ;
	}

	@Override
	public JsonCommon<?> updateFile(String fileId, String fileName, String remoteFileName, String downloadPath,
			String entityTbl, String entityId, String fileFunction) {
		OmsStoreFile storeFile = new OmsStoreFile(); 
		storeFile.setFile_id(fileId);
		storeFile.setDownload_path(downloadPath);
		storeFile.setRemote_file_name(remoteFileName);
		storeFile.setFilename_extension(getExtensionName(fileName));
		storeFile.setFile_name(fileName);
		storeFile.setEntity_tbl(entityTbl);
//		storeFile.setValid_flag("1");
		storeFile.setFile_function(fileFunction);
		storeFile.setEntity_id(entityId);
		try 
		{
			this.updateFile(storeFile);
		} 
		catch (Exception e) 
		{
//			e.printStackTrace();
			this.logError("update file errror", e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon(storeFile) ;
	}

	@Override
	public JsonCommon<?> deleteFile(String fileId) 
	{
		try 
		{
			super.storeFileDaoImpl.deleteById(fileId, OmsStoreFile.class);
		} 
		catch (OmsDaoException e) 
		{
			this.logError("delete file errror", e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon("success") ;
	}

	@Override
	public JsonForList<?> findPageOrAll(OmsCommonParam params, OmsStoreFile findCondition) {
		try 
		{
			List<OmsStoreFile> lst = storeFileDaoImpl.pagingFind(params, findCondition);
			int total = 0;
			if (params.isQueryTotal())
			{
				total = storeFileDaoImpl.queryTotalByObject(findCondition);
			}
			return this.buildSuccessJsonForList(params, lst, total);
		}
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

}